package hy.service;

import hy.beans.Result;
import hy.beans.User;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SystemService {
    List<User> users;
    {
        users = new ArrayList<>();
        users.add(new User(1,"a1",new Date()));
        users.add(new User(2,"a2",new Date()));
    }
    public Result getUserById(Integer id) {
        Result rs = new Result();
        for (User user : users) {
            System.out.println("========================="+user);
            if(user.getId().equals(id)){
                rs.setObj(user);
                rs.setRs(true);
            }
        }

        return rs;
    }

    public Result addUser(User u) {
        users.add(u);
        Result r = new Result();
        r.setRs(true);
        return r;
    }

    public Result modifyUserById(User u) {
        Result r = new Result();
        for (User user : users) {
            if(user.getId().equals(u.getId())){
                user.setName(u.getName());
                user.setBirth(u.getBirth());
                r.setRs(true);
                r.setObj(u.getName());
                break;
            }
        }
        return r;

    }
 
    public Result deleteUserById(Integer id) {
     Result r = new Result();
        for (int i=0;i<users.size();i++) {
         if(users.get(i).getId().equals(id)){
                users.remove(i);
                r.setRs(true);
              break;
            }
      }
        return r;
    }
}